﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UIT.Web.Controls;
using UIT.Controls.Containers;

namespace UIT.Web.Renderers
{
    public class FormRenderer : ContainerRenderer, IControlRenderer<Form>
    {
        private System.Xml.XmlWriter writer;
        #region IWebRenderer Members

        public override void Initialize(RendererManager manager, object userObject)
        {
            base.Initialize(manager, userObject);
            this.writer = userObject as System.Xml.XmlWriter;
        }

        #endregion

        #region IRenderer<Form> Members

        public void Render(Form control)
        {
            writer.WriteStartElement("form");
            if (!string.IsNullOrEmpty(control.Method))
                writer.WriteAttributeString("method", control.Method);
            if (!string.IsNullOrEmpty(control.Action))
                writer.WriteAttributeString("action", control.Action);
            writer.WriteStartElement("input");
            writer.WriteAttributeString("name", "__EVENTTARGET");
            writer.WriteAttributeString("id", "__EVENTTARGET");
            writer.WriteAttributeString("type", "hidden");
            writer.WriteEndElement();
            base.Render((IContainer)control);
            writer.WriteEndElement();
        }

        #endregion

        #region IRenderer Members

        void IControlRenderer.Render(UIT.Controls.IControl child)
        {
            Render(child as Form);
        }

        #endregion

        #region IVisitor<Form> Members

        public void Visit(Form visitable)
        {
            Render(visitable);
        }

        #endregion
    }
}
